package com.jet.member.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jet.core.exception.CustomException;
import com.jet.core.utils.IdUtil;
import com.jet.member.domain.dto.MemberTagDto;
import com.jet.member.domain.entity.MemberTag;
import com.jet.member.mapper.MemberTagMapper;
import com.jet.member.service.IMemberTagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-02
 */
@Service
public class MemberTagServiceImpl extends ServiceImpl<MemberTagMapper, MemberTag> implements IMemberTagService {

    @Override
    public MemberTag create(long merchantNo, MemberTagDto dto) {
        if (existsByName(merchantNo, null, dto.getName())) {
            throw new CustomException("标签已存在");
        }

        MemberTag entity = new MemberTag();
        BeanUtils.copyProperties(dto, entity);
        entity.setId(IdUtil.generateLower());
        entity.setMerchantNo(merchantNo);
        super.save(entity);
        return entity;
    }

    @Override
    public void edit(long merchantNo, String id, MemberTagDto dto) {
        MemberTag tag = super.getById(id);
        if (tag == null) {
            throw new CustomException("标签信息错误");
        }
        if (existsByName(merchantNo, id, dto.getName())) {
            throw new CustomException("标签已存在");
        }
        MemberTag entity = new MemberTag();
        BeanUtils.copyProperties(dto, entity);
        entity.setMerchantNo(merchantNo);
        entity.setId(id);
        super.updateById(entity);
    }

    private boolean existsByName(long merchantNo, String id, String name) {
        Wrapper<MemberTag> wrapper = new LambdaQueryWrapper<MemberTag>()
                .eq(MemberTag::getMerchantNo, merchantNo)
                .eq(MemberTag::getName, name)
                .ne(StrUtil.isNotBlank(id), MemberTag::getId, id);
        return super.exists(wrapper);
    }

    @Override
    public MemberTag findByName(long merchantNo, String name) {
        Wrapper<MemberTag> wrapper = new LambdaQueryWrapper<MemberTag>()
                .eq(MemberTag::getMerchantNo, merchantNo)
                .eq(MemberTag::getName, name);
        return super.getOne(wrapper);
    }

    @Override
    public List<MemberTag> findAllByIds(long merchantNo, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        Wrapper<MemberTag> wrapper = new LambdaQueryWrapper<MemberTag>()
                .eq(MemberTag::getMerchantNo, merchantNo)
                .in(MemberTag::getId, ids);
        return super.list(wrapper);
    }

    @Override
    public List<MemberTag> findAll(long merchantNo) {
        Wrapper<MemberTag> wrapper = new LambdaQueryWrapper<MemberTag>()
                .eq(MemberTag::getMerchantNo, merchantNo);
        return super.list(wrapper);
    }
}
